package cn.stylefeng.guns.sys.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {
	
	/**
	 * 获取默认的时间字符串 ，形如2020-09-01 11:11:11
	 * @return
	 */
	public static String getDefault(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = sdf.format(new Date());
		return now;
	}


	public static Date getTime(String t) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			return sdf.parse(t);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 计算两个时间的长度
	 * @param t1
	 * @param t2
	 * @return
	 */
	public static double timeLong(String t1,String t2){
		Date time1 = getTime(t1);
		Date time2 = getTime(t2);
		double ll=Double.valueOf(time2.getTime() - time1.getTime());
		return ll/1000/3600;
	}

	/**
	 * 获取上月第一天
	 */
	public static String getLastMonthFirst(){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return format.format(calendar.getTime());
	}
	/**
	 * 获取上月最后一天
	 * @return
	 */
	public static String getLastMonthLast(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
//		int month=calendar.get(Calendar.MONTH);
		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return sf.format(calendar.getTime());
	}


	/**
	 * 获取当前月的上个月
	 *
	 * @author liuy
	 * @Date 2020-06-03
	 */
	public static String getLastMonthStr() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		// 设置为当前时间
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);
		date = calendar.getTime();
		String accDate = format.format(date);
		return accDate;
	}

	/**
	 * 获取此月第一天
	 */
	public static String getMonthFirstDayStr(Date date){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		// 设置为预设的时间
		calendar.setTime(date);
//		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return format.format(calendar.getTime());
	}

	/**
	 * 获取此月最后一天
	 */
	public static String getMonthLastDayStr(Date date){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		// 设置为预设的时间
		calendar.setTime(date);
//		calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return format.format(calendar.getTime());
	}
	/**
	 * 获取这个月有多少天,然后存为字符，列表；
	 */
	public static List<String> getMonthLastDayStrNum(Date date) {
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM");
		String month = format2.format(date);
		Calendar calendar=Calendar.getInstance();
		// 设置为预设的时间
		calendar.setTime(date);
		List<String> list = new ArrayList<>();
		for(int i=0;i<calendar.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
			String day = String.valueOf(i+1);
			if(i<10){
				day = "0"+day;
			}
			list.add(month+"-"+day);
		}
		return list;
	}


	/**
	 * 传过来日期的前一天
	 *
	 * @return
	 */

	public static String getDateYestoday(Date date) {
		Calendar calendar = Calendar.getInstance();
		// 设置为预设的时间
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(calendar.getTime());
		return yesterday;
	}

	/**
	 * 传过来日期的后一天
	 *
	 * @return
	 */

	public static String getDateTomorrow(Date date) {
		Calendar calendar = Calendar.getInstance();
		// 设置为预设的时间
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(calendar.getTime());
		return yesterday;
	}
}
